package com.kfm.demo.loginmodel.controller;


import com.kfm.demo.loginmodel.domain.Users;
import com.kfm.demo.loginmodel.mapper.UsersMapper;
import com.kfm.demo.utils.Constant;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
public class LoginController {

    @Autowired
    private UsersMapper usersMapper;

    @GetMapping({"/", "/login"})
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response) {
        ModelAndView index = new ModelAndView("login");
        Cookie[] cookies = request.getCookies();
        if (cookies!= null || cookies.length == 0) {
            Cookie cookie = new Cookie("counter", "1");
            cookie.setMaxAge(60 * 60 * 24 * 30); // 设置cookie的有效期为30天
            response.addCookie(cookie);
            index.getModel().put("cookieValue",cookie.getValue());
        }else {
            for (Cookie cookie : cookies)
                if (cookie.getName().equals("counter")) {
                    int value = Integer.parseInt(cookie.getValue());
                    value++;
                    cookie.setValue(String.valueOf(value));
                    response.addCookie(cookie);
                    index.getModel().put("cookieValue",cookie.getValue());
                }
        }
        return index;
    }

    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {

        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        // 生成的验证码
        String text = specCaptcha.text();

        request.getSession().setAttribute(Constant.CAPTCHA, text);

        specCaptcha.out(response.getOutputStream());
    }

    @PostMapping("/email")
    public Map<String, Object> login() {


        return null;
    }


    @PostMapping("/logins")
    public Map<String, Object> exampleResponseMethod(@RequestParam("username") String username,
                                                     @RequestParam("password") String password,
                                                     @RequestParam("captcha") String captcha,
                                                     HttpServletRequest request
    )
    {
        // 模拟API返回的数据
        Map<String, Object> data = new HashMap<>();
        HttpSession session = request.getSession();
        String attribute = (String) session.getAttribute(Constant.CAPTCHA);
        if (!((String) session.getAttribute(Constant.CAPTCHA)).equalsIgnoreCase(captcha)) {
            data.put("code", "408");
            data.put("status", "error");
            data.put("message", "验证码错误");
            return data;
        }

        Users users = new Users();
        users.setUsername(username);
        users.setPasswordhash(password);
        Users datausers = usersMapper.selectByUser(users);

        if (datausers == null) {
            data.put("code", "401");
            data.put("status", "error");
            data.put("message", "用户名或密码错误");
            return data;
        } else if (datausers.getPasswordhash().equals(password) && datausers.getUsername().equals(username)) {
            data.put("code", "200");
            data.put("status", "success");
            data.put("message", "登录成功");
        } else {
            data.put("code", "500");
            data.put("status", "error");
            data.put("message", "未知错误");
        }

        return data;
    }


}
